﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;


namespace Microhard.Framework.MVCHelper
{
    public class ScriptSubmitDeleteButton : BaseButton
    {
        private string _actionName;
        private string _controllerName;
        public string CallBackScriptFunctionName { set; get; }
        public string SetValueFunctionName { set; get; }

        public ScriptSubmitDeleteButton()
        {
            this._assetKey = SysConst.OPERATION_DELETE;
        }

        public override System.Web.Mvc.MvcHtmlString WriteHtml(System.Web.Mvc.HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            this._actionName = actionName;
            this._controllerName = controllerName;

            return base.CreateButton(SysConst.OPERATION_DELETE, helper, this.ID, linkText, false, null, null, null);
        }

        public override string WriteJQueryReadyInnerScript(HttpContextBase helper)
        {
            StringBuilder script = new StringBuilder();
            script.Append("$('#" + this._id + "').click(function () {");
            script.Append("var identities = " + this.SetValueFunctionName + ";");
            script.Append("if(identities == null){alert('请选择需要删除的数据');return;}");
            script.Append(" var users = $.toJSON(identities);");
            script.Append(" if(confirm(\"您确定执行删除操作吗?\")){");
            script.Append(" $.ajax({");
            string url = "~/" + this._controllerName + "/" + this._actionName;
            if (!string.IsNullOrEmpty(this._areaName))
            {
                url = "~/" + this._areaName + "/" + this._controllerName + "/" + this._actionName;
            }
            url = UrlHelper.GenerateContentUrl(url, helper);
            script.Append(" url: '" + url + "',");
            script.Append(" type: 'POST',");
            script.Append(" dataType: 'json',");
            script.Append(" data: users,");
            script.Append(" contentType: 'application/json; charset=utf-8',");
            script.Append(" success: function (data) { ");
            script.Append(this.CallBackScriptFunctionName);
            script.Append(";}});");
            script.Append("}");
            script.Append("});");

            return script.ToString();
             
        }
    }
}
